package com.chen.bpsptsystem.service;

import com.chen.bpsptsystem.common.ServerResponse;
import com.chen.bpsptsystem.dao.InterestMapper;
import com.chen.bpsptsystem.po.Interest;
import com.chen.bpsptsystem.po.InterestExample;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 兴趣分类
 *
 * @author chen
 */
@Service
public class IInterestService {

    @Resource
    private InterestMapper interestMapper;

    public ServerResponse add(Interest interest) {
        InterestExample example = new InterestExample();
        InterestExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(interest.getName());
        List<Interest> list = interestMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return ServerResponse.createByErrorMsg("已存在");
        }
        int i = interestMapper.insertSelective(interest);
        if (i > 0) {
            return ServerResponse.createBySuccess(interest);
        }
        return ServerResponse.createByErrorMsg("新增失败");
    }


    public ServerResponse list() {
        return ServerResponse.createBySuccess(interestMapper.selectByExample(null));
    }

    public ServerResponse delete(Integer id) {
        int row = interestMapper.deleteByPrimaryKey(id);
        if (row > 0) {
            return ServerResponse.createBySuccessMsg("删除成功");
        }
        return ServerResponse.createByErrorMsg("删除失败");
    }

    public ServerResponse edit(Interest interest) {
        int row = interestMapper.updateByPrimaryKeySelective(interest);
        if (row > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMsg("修改失败");
    }

}
